/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sira.textrankresume.gui;

import com.sira.textrankresume.Grafo;
import com.sira.textrankresume.Sentencia;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseGraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.PluggableGraphMouse;
import edu.uci.ics.jung.visualization.control.ScalingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.TranslatingGraphMousePlugin;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;

/**
 *
 * @author JuanmaSP
 */
public class GraphJDialog extends JDialog{
    Layout<String, Vertice> layout;
    VisualizationViewer<String, Vertice> vv;

    public GraphJDialog(java.awt.Frame parent, boolean modal, Grafo grafo) {
        super(parent, modal);

        this.setSize(600, 600);

        layout = new FRLayout(adaptarGrafo(grafo));
        layout.setSize(this.getSize());

        vv = new VisualizationViewer<String, Vertice>(layout);
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
        vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Vertice>());
        vv.setSize(this.getSize());

        PluggableGraphMouse pgm = new PluggableGraphMouse();
        pgm.add(new TranslatingGraphMousePlugin());
        pgm.add(new ScalingGraphMousePlugin(new CrossoverScalingControl(), 0));

        vv.setGraphMouse(pgm);

        this.getContentPane().add(vv);
    }

    private Graph<String, Vertice> adaptarGrafo(Grafo g){
        Graph<String, Vertice> grafo = new SparseGraph<String, Vertice>();
        List<Sentencia> lista = g.getVertices();
        List<String> listaString = new ArrayList<String>();

        Iterator<Sentencia> it = lista.iterator();

        //Recopilamos el nombre de los vértices en una nueva lista.
        int k = 1;
        while(it.hasNext()){
            Sentencia aux = it.next();
            if(aux.getTexto().length() > 15)
                listaString.add(k + "." + aux.getTexto().substring(0, 15) + "... (" + aux.getPuntuacion() + ")");
            else
                listaString.add(k + "." + aux.getTexto() + " (" + aux.getPuntuacion() + ")");
            k++;
        }

        //Incluimos los vértices en el grafo
        Iterator<String> it2 = listaString.iterator();
        while(it2.hasNext())
            grafo.addVertex(it2.next());

        //Añadimos las aristas
        float[][] matriz = g.getMatrizAdyacencia();
        for(int i = 0; i < matriz.length; i++){
            for(int j = 0; j < i; j++){
                if(matriz[i][j] > 0.0f)
                    grafo.addEdge(new Vertice(matriz[i][j]), listaString.get(i), listaString.get(j));
            }
        }

        //Devolvemos el grafo resultante
        return grafo;
    }

    class Vertice{
        public float valor;

        public Vertice(float v){
            this.valor = v;
        }

        @Override
        public String toString(){
            return Float.toString(valor);
        }
    }
}
